import {
  Body,
  Controller,
  Get,
  Inject,
  Post,
  Provide,
  Query,
  ALL,
  Put,
  Param,
  Del,
} from '@midwayjs/core';
import { UserDTO } from '../dto/user';
import { UserService } from '../service/user';
import { ApiOperation, ApiTags } from '@midwayjs/swagger';

@Provide()
@Controller('/user')
@ApiTags('用户模块')
export class UserController {
  @Inject()
  userService: UserService;

  @Post('/create')
  @ApiOperation({
    summary: '创建用户',
    description: '该模块包含与用户相关的操作',
  })
  async create(@Body(ALL) data: UserDTO) {
    return await this.userService.create(data.toEntity());
  }

  @Put('/edit')
  @ApiOperation({ summary: '用户修改' })
  async edit(@Body(ALL) data: UserDTO) {
    const user = await this.userService.getById(data.id);
    return await this.userService.edit(user);
  }

  @Del('/:id')
  @ApiOperation({ summary: '删除用户' })
  async remove(@Param('id') id: string) {
    const user = await this.userService.getById(id);
    await this.userService.remove(user);
  }

  @Get('/:id', { description: '' })
  @ApiOperation({ summary: '根据id查询用户' })
  async getById(@Param('id') id: string) {
    return await this.userService.getById(id);
  }

  @Get('/page')
  @ApiOperation({ summary: '根据分页获取用户' })
  async page(@Query('page') page: number, @Query('size') size: number) {
    return await this.userService.page(page, size);
  }

  @Get('/list')
  @ApiOperation({ summary: '获取所有用户' })
  async list() {
    return await this.userService.list();
  }
}
